package com.xindaoapp.happypet.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.fragments.ForgetPasswordFragment;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.LoginManager;
import com.xindaoapp.happypet.utils.MoccaPreferences;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 登录<br/>
 * 
 * 使用方法<br/>
 * Intent intent = new Intent(context, com.xindaoapp.happypet.activity.MainActivity.class);<br/>
 * intent.putExtra(Constants.KEY_SKIP_CLASS_NAME, LoginFragment.class.getCanonicalName());<br/>
 * startActivity(intent);<br/>
 * 
 * @author chenghao
 * 
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
    private EditText vUserName;
    private EditText vPassword;
    private View vLogin;
    private View vQQLogin;
    private View vWeiboLogin, vWeixinLogin;

    private LoginManager mLoginManager;

    private LoginReceiver mLoginReceiver;

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_login;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        vLogin.setOnClickListener(this);
        vQQLogin.setOnClickListener(this);
        vWeiboLogin.setOnClickListener(this);
        vWeixinLogin.setOnClickListener(this);
        findViewById(R.id.forget_password).setOnClickListener(this);
        mLoginReceiver = new LoginReceiver();
        registerReceiver(mLoginReceiver, new IntentFilter(Constants.ACTION_LOGIN_STATE_CHANGED));

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoginReceiver);
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTopBarTitle("登录");
        vUserName = (EditText) findViewById(R.id.username);
        vPassword = (EditText) findViewById(R.id.password);
        vLogin = findViewById(R.id.login);
        vQQLogin = findViewById(R.id.login_qq);
        vWeiboLogin = findViewById(R.id.login_sina);
        vWeixinLogin = findViewById(R.id.login_weixin);

        mLoginManager = new LoginManager(this);
        if (!TextUtils.isEmpty(MoccaPreferences.UserName.get())
                && !TextUtils.isEmpty(MoccaPreferences.PassWord.get())) {
            vUserName.setText(MoccaPreferences.UserName.get());
            vPassword.setText(MoccaPreferences.PassWord.get());
        }

    }

    private boolean isInputed() {
        if (TextUtils.isEmpty(vUserName.getText().toString())) {
            Toast.makeText(LoginActivity.this, R.string.input_username_null, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        if (TextUtils.isEmpty(vPassword.getText().toString())) {
            Toast.makeText(LoginActivity.this, R.string.input_password_null, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                if (isInputed()) {
                    mLoginManager.login(
                            ProgressHUD.show(LoginActivity.this, "正在登录请稍候...", true, true, null),
                            vUserName.getText().toString(), vPassword.getText().toString());
                }
                break;
            case R.id.login_qq:
                mLoginManager.loginQQ(true);
                break;
            case R.id.login_sina:
                mLoginManager.loginSina(true);
                break;
            case R.id.login_weixin:
                mLoginManager.loginWeixin(true);
                break;
            case R.id.forget_password:
                // startFragment(new ForgetPasswordFragment());
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        ForgetPasswordFragment.class.getCanonicalName());
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (LoginManager.mSsoHandler != null) {
            LoginManager.mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    class LoginReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }

    }

}
